﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAbstractor;
using DataAbstractorExample.Forms;
using DataAbstractorExample.Models;

namespace DataAbstractorExample.Controllers
{
    public class CommentController : Controller
    {
        [HttpPost]
        public ActionResult Add(int id, CommentAdd model)
        {
            var context = new DataContext();
            var note = context.Notes.FirstOrDefault(i => i.NoteId == id);
            if (note != null)
            {
                var comment = model.To<Comment>();
                comment.Note = note;
                note.Comments.Add(comment);
                context.SaveChanges();
            }

            return RedirectToAction("Index", "Note");
        }

        public ActionResult Remove(int id)
        {
            var context = new DataContext();
            var comment = context.Comments.FirstOrDefault(i => i.CommentId == id);
            if (comment != null)
            {
                context.Comments.Remove(comment);
                context.SaveChanges();
            }

            return RedirectToAction("Index", "Note");
        }
    }
}
